package cn.csg.ams.collect2.poptopics.infrastructure.utils;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtil {

    static ObjectMapper objectMapper = new ObjectMapper();

    private JsonUtil() {
    }

    public static String toJson(Object object) {
        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            return null;
        }
    }

    public static <T> T toObject(String json, Class<T> type) {
        if (StrUtil.isNotBlank(json)) {
            try {
                return objectMapper.readValue(json, type);
            } catch (JsonProcessingException e) {
                return null;
            }
        }
        return null;
    }


}
